<template>
  <view>
    <view class="goods-list">
      <view v-for="(item, i) in goodList" :key="i" @click="gotoDetail(item)">
        <my-goods :goods="item"></my-goods>
      </view>
    </view>
  </view>
</template>


<script>
	export default {
		data() {
			return {
         // 请求参数对象
        queryObj: {
          // 查询关键词
          query: '',
          // 商品分类Id
          cid: '',
          // 页码值
          pagenum: 1,
          // 每页显示多少条数据
          pagesize: 10,
          },
          goodList:[],
          total:0,
          isLoading:false
			}
		},
    onLoad(options) {
      this.queryObj.query = options.query || ''
      this.queryObj.cid = options.cid || ''
      // console.log(options)
      this.getGoodList()
    },
    methods:{
      async getGoodList(cb) {
        this.isLoading = true
        const {data: res} = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
        // console.log(res)
        cb && cb()
        if(res.meta.status !== 200) return uni.$showMsg()
        // 为数据赋值：通过展开运算符的形式，进行新旧数据的拼接
        this.goodList = [...this.goodList,...res.message.goods]
        this.total = res.message.total
        this.isLoading = false
      },
      gotoDetail(item) {
        // console.log('11')
        uni.navigateTo({
          url:'../goods_detail/goods_detail?goods_id=' + item.goods_id
        })
      }
    },
    // 监听下拉触底事件
    onReachBottom() {
      if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) return uni.$showMsg('没有商品了！o(╥﹏╥)o')
      if(this.isLoading) {
        return
      }else{
        this.queryObj.pagenum += 1
        this.getGoodList()
      }
    },
    // 监听上拉刷新事件
    onPullDownRefresh() {
      this.queryObj.pagenum = 1
      this.isLoading = false
      this.total = 1
      this.goodList = []
      
      // 重新发请求，传一个回调函数，关闭下拉刷新效果
      this.getGoodList(() => uni.stopPullDownRefresh())
    }
	}
</script>

<style lang="scss">

</style>
